#pragma once

#include "adalog/LogAppender.h"
#include "adalog/buffer/BufferPoolManager.h"
#include "adalog/buffer/WriteBufferGuard.h"
#include "adalog/util/Channel.h"
#include "adalog/util/ThreadPool.h"
#include <list>
#include <thread>
#include <atomic>

namespace adalog
{

    class AsyncProxyAppender : public LogAppender
    {
    public:
        AsyncProxyAppender(std::list<LogAppender::Ptr> appenders);
        ~AsyncProxyAppender();
        void Append(const char* data, size_t len) override;

    private:
        void ConsumerThread();

    private:
        std::list<LogAppender::Ptr> appenders_;
        Channel<size_t> free_buffer_queue_;
        Channel<size_t> full_buffer_queue_;
        WriteBufferGuard current_buffer_;
        BufferPoolManager buffer_pool_;
        std::thread consumer_;
        ThreadPool thread_pool_;
        std::atomic<bool> stop_{false};
    };

} // namespace adalog